// pages/bianminone/index.js
const request = require('../../utils/request');

Page({
  data: {
    typeName: '',
    list: [],
    noData: false
  },

  onLoad(options) {
    // 获取传入的类型名称
    const typeName = options.text;
    
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: typeName
    });
    
    this.setData({
      typeName: typeName
    });
    
    this.loadData();
  },

  // 加载数据
async loadData() {
  try {
    // 显示加载状态
    wx.showLoading({ title: '加载中...' });
    const {typeName} = this.data;
    
    // 构建类型名称到数字值的映射
    const typeNameToValue = {
      '便民电话': 0,
      '政务服务': 1,
      '物业服务中心': 2,
      '家政服务': 3,
      '小区民警': 4,
      '周边医院': 5,
      '周边商铺': 6,
      '充值缴费': 7,
      '其他': 8
    };
    
    // 获取对应的数字值
    const leixinValue = typeNameToValue[typeName];
    
    if (leixinValue === undefined) {
      wx.hideLoading();
      this.setData({
        list: [],
        noData: true
      });
      return;
    }
    
    // 根据类型值查询数据
    const res = await request.get("/ruoyi-hqy/community_policeman_hqy/list", {
      leixin: leixinValue
    });
    console.log(res);
    if (res.code === 200) {
      this.setData({
        list: res.rows || [],
        noData: res.rows.length === 0
      });
    } else {
      wx.showToast({
        title: '数据加载失败',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('数据加载异常:', error);
    wx.showToast({
      title: '请求异常，请重试',
      icon: 'none'
    });
  } finally {
    wx.hideLoading();
  }
},

  // 拨打电话
  makeCall(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    });
  }
});